using UnityEngine;
using System.Collections;

/**
 * Script of menu controller in pause time.
 * The purpose of this class is to manage controls while timescale is 0.
 * @author Benjamin Bruneau
 */
public class PauseControllerScript : MonoBehaviour
{
	
	private const string REPLAY_MODE_NAME = "ReplayMode"; // Replay Mode GameObject's name	
	private const string REPLAY_PAUSED = "Replay Paused"; // Replay Paused Text
	private const string RESUME_REPLAY = "Resume Replay"; // Resume Replay Text
	private const string SELECTED_RESUME_REPLAY = "[ Resume Replay ]"; // Selected Replay Mode Field
	private const string REPLAY_MODE = "ReplayMode"; // Replay Mode Field

	private GameObject _replayMode; // Replay Mode GameObject
	
	public GameObject _pauseBackground; // Pause Background GameObject
	public GameObject _gamePausedText; // Game Paused text GameObject
	public GameObject _selectableTexts; // Selectable Texts GameObject
	public GameObject _targetsTexts; // Non Selectable Texts GameObject
	public GameObject _resumeText; // Resume Text GameObject
	public GameObject _medalGold; // Gold Medal in Pause
	public GameObject _medalSilver; // Silver Medal in Pause
	public GameObject _medalBronze; // Bronze Medal in Pause
	
	public bool _pause; // Game state
	
	/**
	 * Called on initialization.
	 * @return void
	 */
	void Start ()
	{
		
		//Initialized because this object moves through scenes
		_replayMode = GameObject.Find (REPLAY_MODE_NAME);
		
		_pause = false;
		manageReplayMode ();
	}
	
	/**
	 * Called once per frame.
	 * @return void
	 */
	void Update ()
	{	
				
		//Pause mode
		if (Input.GetKeyUp (KeyCode.Escape))
		{
			if (!_pause)
			{
				renderAndActivate (true);
				Time.timeScale = 0;
			} else {
				renderAndActivate (false);
				Time.timeScale = 1;				
			}
		}
	}
	
	/**
	 * Public unpause (if player leaves level by pause menu).
	 * @return void
	 */
	public void unpause ()
	{
		renderAndActivate (false);
		Time.timeScale = 1;
	}
	
	/**
	 * Displays pause menu.
	 * @param bool
	 * @return void
	 */
	private void renderAndActivate (bool pActivation)
	{
		// Pause true or false
		_pause = pActivation;
		
		// Static Mesh Renderers activation
		_pauseBackground.GetComponent<MeshRenderer> ().enabled = pActivation;
		_gamePausedText.GetComponent<MeshRenderer> ().enabled = pActivation;
		
		// Medals
		if (!_replayMode.GetComponent<ReplayModeScript> ()._replayMode) 
			{
			_medalGold.GetComponent<MeshRenderer> ().enabled = pActivation;
			_medalSilver.GetComponent<MeshRenderer> ().enabled = pActivation;
			_medalBronze.GetComponent<MeshRenderer> ().enabled = pActivation;
			MeshRenderer[] tabRendererTarget = _targetsTexts.GetComponentsInChildren<MeshRenderer> ();
			foreach (MeshRenderer meshRenderer in tabRendererTarget) 
			{ 
				meshRenderer.enabled = pActivation;
			}
		}
		
		// Menu controls activation
		_selectableTexts.GetComponent<MenuControllerScript> ().enabled = pActivation;
		
		// Get children parameters
		
		MeshRenderer[] tabRenderer = _selectableTexts.GetComponentsInChildren<MeshRenderer> ();
		BoxCollider[] tabBoxCollider = _selectableTexts.GetComponentsInChildren<BoxCollider> ();	
		DetectClickOnObjectScript[] tabDetectClickOnObjectScript = _selectableTexts.GetComponentsInChildren<DetectClickOnObjectScript> ();	
		
		// Mesh renderer activation
		foreach (MeshRenderer meshRenderer in tabRenderer) 
		{ 
			meshRenderer.enabled = pActivation;
		}
		
		
		
		// Box Collider activation
		foreach (BoxCollider boxCollider in tabBoxCollider) 
		{ 
			boxCollider.enabled = pActivation;
		}
		
		// Script activation
		foreach (DetectClickOnObjectScript detectClickOnObjectScript in tabDetectClickOnObjectScript) 
		{ 
			detectClickOnObjectScript.enabled = pActivation;
		}
		
	}
	
	/**
	 * Displays pause menu in replay mode.
	 * @param bool
	 * @return void
	 */
	private void manageReplayMode ()
	{
		//If replay mode ON
		if (_replayMode.GetComponent<ReplayModeScript> ()._replayMode) 
		{
			//Change texts
			_gamePausedText.GetComponent<TextMesh> ().text = REPLAY_PAUSED;
			_resumeText.GetComponent<DetectClickOnObjectScript> ()._textOriginal = RESUME_REPLAY;
			_resumeText.GetComponent<DetectClickOnObjectScript> ()._textSelected = SELECTED_RESUME_REPLAY;
			_resumeText.GetComponent<TextMesh> ().text = RESUME_REPLAY;
		}
	}
}
